package com.study.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.study.app.domain.bo.AppImageBo;
import com.study.app.domain.vo.AppImageVo;
import com.study.app.service.IAppImageService;
import com.study.common.annotation.Log;
import com.study.common.annotation.RepeatSubmit;
import com.study.common.core.controller.BaseController;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.validate.AddGroup;
import com.study.common.core.validate.EditGroup;
import com.study.common.enums.BusinessType;
import com.study.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * App图片管理
 *
 * @author study
 * @date 2024-05-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/image")
public class AppImageController extends BaseController {

    private final IAppImageService iAppImageService;

    /**
     * 查询App图片管理列表
     */
    @SaCheckPermission("system:image:list")
    @GetMapping("/list")
    public TableDataInfo<AppImageVo> list(AppImageBo bo, PageQuery pageQuery) {
        return iAppImageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出App图片管理列表
     */
    @SaCheckPermission("system:image:export")
    @Log(title = "App图片管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppImageBo bo, HttpServletResponse response) {
        List<AppImageVo> list = iAppImageService.queryList(bo);
        ExcelUtil.exportExcel(list, "App图片管理", AppImageVo.class, response);
    }

    /**
     * 获取App图片管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:image:query")
    @GetMapping("/{id}")
    public R<AppImageVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(iAppImageService.queryById(id));
    }

    /**
     * 新增App图片管理
     */
    @SaCheckPermission("system:image:add")
    @Log(title = "App图片管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppImageBo bo) {
        return toAjax(iAppImageService.insertByBo(bo));
    }

    /**
     * 修改App图片管理
     */
    @SaCheckPermission("system:image:edit")
    @Log(title = "App图片管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppImageBo bo) {
        return toAjax(iAppImageService.updateByBo(bo));
    }

    /**
     * 删除App图片管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:image:remove")
    @Log(title = "App图片管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppImageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}